package com.jsmy.marketing.fg;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.jsmy.marketing.R;
import com.jsmy.marketing.adapter.ComFragmentAdapter;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import butterknife.BindView;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/17.
 * Time: 17:48.
 * To change for file | settings | file and code templates.
 */

public class BusinessFragment extends BaseFragment implements UMShareListener {


    @BindView(R.id.m_viewpage)
    ViewPager mViewpage;
    @BindView(R.id.m_tab)
    TabLayout mTab;
    ComFragmentAdapter adapter;
    BaseFragment currentFragment;

    PlantFragment plantFragment = new PlantFragment();
    ListenerFragment listenerFragment = new ListenerFragment();
    DailyFragment dailyFragment = new DailyFragment();

    @Override
    protected int resLayout() {
        return R.layout.fg_bussiness_layout;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        adapter = new ComFragmentAdapter(getChildFragmentManager(), new String[]{"沟通计划", "试听列表", "沟通日志"}) {

            @Override
            public Fragment getItem(int postion) {
                switch (postion) {
                    case 0:
                        if (plantFragment == null) {
                            plantFragment = new PlantFragment();
                        }
                        currentFragment = plantFragment;
                        break;
                    case 1:
                        if (listenerFragment == null) {
                            listenerFragment = new ListenerFragment();
                        }
                        currentFragment = listenerFragment;
                        break;
                    case 2:
                        if (dailyFragment == null) {
                            dailyFragment = new DailyFragment();
                        }
                        currentFragment = dailyFragment;
                        break;
                }
                return currentFragment;
            }
        };
//        mViewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                switch (position) {
//                    case 0:
//                        if (plantFragment == null) {
//                            plantFragment = new PlantFragment();
//                        }
//                        currentFragment = plantFragment;
//                        break;
//                    case 1:
//                        if (listenerFragment == null) {
//                            listenerFragment = new ListenerFragment();
//                        }
//                        currentFragment = listenerFragment;
//                        break;
//                    case 2:
//                        if (dailyFragment == null) {
//                            dailyFragment = new DailyFragment();
//                        }
//                        currentFragment = dailyFragment;
//                        break;
//
//                }
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
        mTab.setTabMode(TabLayout.MODE_FIXED);
        mViewpage.setAdapter(adapter);
        mViewpage.setOffscreenPageLimit(3);
        mTab.setupWithViewPager(mViewpage);
    }

    @Override
    public void update() {
        switch (mViewpage.getCurrentItem()) {
                    case 0:
                        plantFragment.update();
                        break;
                    case 1:
                         listenerFragment.update();
                        break;
                    case 2:
                       dailyFragment.update();
                        break;

                }
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA share_media) {

    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {

    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {

    }

    @Override
    public void refreshData(Object response, String position) {

    }
}
